package edu.sjsu.itack.dao;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

import edu.sjsu.itack.entity.Board;
import edu.sjsu.itack.util.ITackException;

public class BoardDao {
	
	/**
	 * Creates a Board using the pojo provided as parameter
	 * @param board
	 * @throws ITackException
	 */
	public void createBoard(Board board) throws ITackException
	{
		String query = "insert into itack.board(name,description,type,user) values(?,?,?,?);";
		Connection conn = null;
		conn = ConnectionFactory.getInstance().getConnection();
		try {
			PreparedStatement preparedStatement = conn.prepareStatement(query);
			preparedStatement.setString(1, board.getName());
			preparedStatement.setString(2, board.getDescription());
			preparedStatement.setString(3, board.getType());
			preparedStatement.setLong(4, board.getUserId());
			preparedStatement.executeUpdate();
			preparedStatement.close();
		} catch (SQLException e) {
			throw new ITackException("EXC104", e);
		}	
		finally{
			ConnectionFactory.getInstance().closeConnection(conn);
		}
	}
	
	/**
	 * Delete the board with the specified boardId
	 * @param boardId
	 * @throws ITackException
	 */
	public void deleteBoard(int boardId) throws ITackException
	{
		String query = "delete from itack.Board where id=?;";
		Connection con = null;
		con = ConnectionFactory.getInstance().getConnection(); 
		try {
			PreparedStatement preparedStatement = con.prepareStatement(query);
			preparedStatement.setInt(1, boardId);
			preparedStatement.executeUpdate();
			preparedStatement.close();
		} catch (SQLException e) {
			throw new ITackException("EXC104", e);
		}
		finally
		{
			ConnectionFactory.getInstance().closeConnection(con);
		}
	}
	
	/**
	 * Updates the board details of a given board
	 * @param board
	 * @throws ITackException
	 */
	public void updateBoard(Board board) throws ITackException
	{
		String query = "update itack.board set name=?,description=?,type=? where id=?";
		Connection con = null;
		con = ConnectionFactory.getInstance().getConnection();
		try {
			PreparedStatement preparedStatement = con.prepareStatement(query);
			preparedStatement.setString(1, board.getName());
			preparedStatement.setString(2, board.getDescription());
			preparedStatement.setString(3, board.getType());
			preparedStatement.setInt(4, board.getBoardId());
			preparedStatement.executeUpdate();
		} catch (SQLException e) {
			throw new ITackException("EXC104", e);
		}
		finally{
			ConnectionFactory.getInstance().closeConnection(con);
		}
	}
	
	/**
	 * Gets all the boards for the given user
	 * @param userId
	 * @return
	 * @throws ITackException
	 */
	
	public Board[] getAllBoardsForUser(long userId) throws ITackException
	{
		List<Board> list = new ArrayList<Board>();
		String query = "Select id,name,description,type from itack.board where user=?";
		Connection con = null;
		con = ConnectionFactory.getInstance().getConnection();
		try {
			PreparedStatement preparedStatement = con.prepareStatement(query);
			preparedStatement.setLong(1, userId);
			ResultSet resultSet = preparedStatement.executeQuery();
			if(resultSet.isBeforeFirst())
			{
				while(resultSet.next())
				{
					Board board = new Board();
					board.setBoardId(resultSet.getInt(1));
					board.setName(resultSet.getString(2));
					board.setDescription(resultSet.getString(3));
					board.setType(resultSet.getString(4));
					board.setUserId(userId);
					list.add(board);
				}
			}
			preparedStatement.close();
			resultSet.close();
			return list.toArray(new Board[list.size()]);
		} catch (SQLException e) {
			throw new ITackException("EXC104", e);
		}
		finally{
			ConnectionFactory.getInstance().closeConnection(con);
		}
	}
}
